const app = getApp();
let ServiceUrl = app.globalData.ServiceUrl;
Page({
  data: {
    putFrame: false,//拒绝弹出框
    replyFrame: false,//回复弹出框
    Money:false,//设置金额弹出框
    set_money:'',
    detailObj: {},
    reason:'',
    message:'',
    order_id:'',
    money:'',
    user_id:'',
    role_state:'',
    new_state:'',
    button:false,
    steps: [
      {
        desc: '已提交',
      },
      {
        desc: '管理员审核',
      },
      {
        desc: '平台审核',
      },
      {
        desc: '已付款',
      },
      {
        desc: '已完成',
       },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
//    wx.stopPullDownRefresh();//刷新完成后停止下拉刷新动效
    console.log(options);
    console.log(wx.getStorageSync('roleState'))
    let that=this;
    let order_id = options.order_id;
//    let order_id = '39';
    console.log(order_id)
    that.setData({order_id: order_id})
    that.getOrderInfo(order_id);//发送请求数据获取订单信息
  },
/**
 * 发送请求数据获取订单信息
 * @param {*} order_id 
 */
  getOrderInfo: function(order_id){
    let that = this
    //获取用户user_id
    let user_id = wx.getStorageSync('uid');
    let role_state = wx.getStorageSync('roleState')
    wx.request({
      url: ServiceUrl + 'getOrderAssistance',
      data:{order_id: order_id},
      success:function(res){
        console.log(res)
       // console.log(options.role_state)
        //更新data中detailObj的状态值
        that.setData({
          detailObj: res.data,
          message:res.data.message,
          reason: res.data.reason,
          role_state: role_state,
          new_state: res.data.new_state,
          user_id:user_id,
          order_id: res.data.order_id,
          money:res.data.set_money
          //user_id=res.data.user_id
        })
      },
      fail:function(res){
        wx.showModal({
          title:"提示",
          content:"发送请求失败",
          showCancel: false, //是否显示取消按钮-----》false去掉取消按钮
          confirmColor: 'skyblue', //确定文字的颜色
        })
      }
    })
  },
  /**
   * 付款
   */
  pay:function(){
    let that = this
    wx.request({
//      url: ServiceUrl + 'api/pay/feer',
      url: ServiceUrl + 'payOrderAssistance',
      data: {
        user_id:that.data.user_id,
        order_id: that.data.order_id 
      },
      success:function(res){
      //  console.log(res)
        console.log('调起支付')
        wx.requestPayment({
          nonceStr: res.data.nonceStr,//随机串
          package: res.data.package,
          paySign: res.data.paySign,//微信签名
          timeStamp: res.data.timeStamp,//时间戳，自1970年以来的秒数
          signType:'MD5',//微信签名方式
          success: function(res){
            console.log(res);
            that.setData({
              new_state:3
            })
            wx.showModal({
              title:"提示",
              content:"支付成功,将尽快为您安排服务，请留意订单状态中的回复信息！",
              showCancel: false,
              confirmColor: 'skyblue', //确定文字的颜色
            })
            //修改订单状态
            wx.request({
              //url: ServiceUrl + 'api/pay/payEnd',
              url: ServiceUrl + 'payOrderAssistanceEnd',
              data:{
                order_id:that.data.order_id
              },
              success:function(res){
                console.log(res)
                if(res.data.state == 200){
                  that.setData({
                    button:true
                  })
                  //支付成功，给老汤发通知
                  that.sendBossPayMsg(that.data.order_id)
                }
              },
              fail:function(res){
                wx.showModal({
                  title:"提示",
                  content:"发送请求失败(修改支付状态)",
                  showCancel: false,
                  confirmColor: 'skyblue',
                })
              }
            })
          },
          fail:function(res){
          //   wx.showModal({
          //     title:"提示",
          //     content:"发送请求失败(调起支付)",
          //     showCancel: false,
          //     confirmColor: 'skyblue',
          //   })
          }
        })
      },
      fail:function(res){
        wx.showModal({
          title:"提示",
          content:"发送请求失败(调起支付前)",
          showCancel: false,
          confirmColor: 'skyblue',
        })
      }
    })
  },
  
  /**
   * 老汤完成订单
   */
  finishOrder:function(){//弹出回复消息框
    let that = this
    //console.log('订单id为：'+that.data.order_id)
    that.setData({replyFrame:true});
  },
  closeReply:function(){
    this.setData({replyFrame:false})
  },
  
  //老汤完成服务，向管理员发送完成服务提醒的动作
  sendFinishManage(){
    let that = this
    wx.request({
      url: ServiceUrl + 'sendReplyManage',
      data: {
        order_id: that.data.order_id
      },
      method: 'get',
      success: function(res){
        if(res.data.state == 200){
          wx.showModal({
                title: '提示',
                content: '您确定要发送吗？',
                showCancel: true, //是否显示取消按钮-----》false去掉取消按钮
                cancelText: "否", //默认是“取消”
                cancelColor: 'skyblue', //取消文字的颜色
                confirmText: "是", //默认是“确定”
                confirmColor: 'skyblue', //确定文字的颜色
                success: function(res) {
                    if (res.cancel) {//点击取消
                        console.log("您点击了取消")
                    } else if(res.confirm){//点击确定
                        console.log("您点击了确定")                        
                        //调起发送恢复数据的动作
                        that.finishReply();
                        that.setData({replyFrame:false})
                    }
                }
          })
        }else{
          wx.showModal({
            title:"提示",
            content:res.data.msg,
            showCancel:false
          })
        }
      }
    })
  },
  //老汤完成服务向后台发送回复的数据
  finishReply:function(){
    let that = this
    that.editorCtx.getContents({
      success: function (res) {  
        console.log(res.text)
        let message = res.text    
        wx.request({
          url: ServiceUrl + 'finishOrderAssistance',
          data:{
            order_id: that.data.order_id,
            message: message
          },
          success:function(res){
            if(res.data.state == 200){
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
              that.setData({  
                button:true,  
                new_state:4,
                message:message,
                replyFrame: false
              })
            }else{
              wx.showToast({
                title: res.data.msg,
                icon:'none'
              })
            }
          },
          fail:function(res){
            wx.showModal({
              title:'提示',
              content:'发送请求失败',
              showCancel:false
            })
          }
        })
      },
      fail: function (error){
        console.log(error)
      }
    })
  },
//超级管理员通过设置金额
bossPass:function(){//调出设置金额的弹出框
  let that=this;
  that.setData({Money:true});
},
//关闭设置金额
closeSetMoney:function(){
  let that=this;
  that.setData({Money:false});
},
//设置付款
formSubmit:function(e){
  console.log(e)
  let that=this;
  console.log('设置金额为：'+e.detail.value.set_money);
  let regPos = /^\d+(\.\d+)?$/;
  if(!regPos.test(e.detail.value.set_money)){
    wx.showToast({
      title: '请输入数字',
      icon: "none"
    })
    return false;
  }
  if(e.detail.value.set_money){
    wx.showModal({
          title: '提示',
          content: '是否确认此设置？',
          showCancel: true, //是否显示取消按钮-----》false去掉取消按钮
          cancelText: "否", //默认是“取消”
          cancelColor: 'skyblue', //取消文字的颜色
          confirmText: "是", //默认是“确定”
          confirmColor: 'skyblue', //确定文字的颜色
          success: function(res) {
              if (res.cancel) {//点击取消
                  console.log("您点击了取消")
              } else if(res.confirm){//点击确定
                  console.log("您点击了确定")                        
                  //调起发送恢复数据的动作
                  that.managePayMsg(that.data.order_id,e.detail.value.set_money);
              }
          }
    })
  }else{
    wx.showToast({
      title: '请输入所设置的金额',
      icon:'none'
    })
  }
},
//设置金额发送后台数据的动作
updateSetMoney:function(money){
  let that = this
  wx.request({
    url: ServiceUrl + 'bossPass',
    data: {
      order_id: that.data.order_id,
      set_money: money
    },
    success: function(res){
      console.log(res)
      if(res.data.state == 200){
        that.setData({
          Money:false,
          new_state:2,
          button:true,
          set_money:money
        })
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })        
        that.setData({Money:false});//关闭弹窗
      }
    },
    fail:function(res){
      wx.showModal({
        title:"提示",
        content:"发送请求失败",
        showCancel: false,
        confirmColor: 'skyblue',
      })
    }
  })
},
/**
 * 管理员审核助医服务通过审核消息给老汤，让他设置金额
 * @param {*} order_id 
 */
sendMsgToBoss:function(order_id){
  wx.request({
    url: ServiceUrl + 'sendMsgToBoss',
    data: {order_id: order_id},
    method: "POST",
    success: function(res){
      console.log(res)
    },
    fail: function(){
      console.log('发送请求失败')
    }
  })
},
//给老汤发送完成付款的通知
sendBossPayMsg:function(order_id){
  wx.request({
    url: ServiceUrl + 'payFinishMsg',
    data: {order_id: order_id},
    method: "POST",
    success: function(res){
      console.log(res)
    },
    fail: function(){
      console.log('发送请求失败')
    }
  })
},
//老汤设置金额成功后给管理员发送待付款消息
managePayMsg:function(order_id,money){
  let that = this
  wx.request({
    url: ServiceUrl + 'payMoneyManage',
    data: {order_id: order_id},
    method: "POST",
    success: function(res){
      console.log(res)
      if(res.data.state == 400){
        wx.showModal({
          title:"提示",
          content:res.data.msg,
          showCancel:false
        })
      }else{
        that.updateSetMoney(money)
      }
    },
    fail: function(){
      console.log('发送请求失败')
    }
  })
},
//公司管理员审核通过
managePass:function()
{
  let that=this;
  //获取订单order_id
  console.log(this.options)
  wx.showModal({
        title: '提示',
        content: '您确定要通过审核吗？',
        showCancel: true, //是否显示取消按钮-----》false去掉取消按钮
        cancelText: "否", //默认是“取消”
        cancelColor: 'skyblue', //取消文字的颜色
        confirmText: "是", //默认是“确定”
        confirmColor: 'skyblue', //确定文字的颜色
        success: function(res) {
            if (res.cancel) {//点击取消
                console.log("您点击了取消")
            } else if(res.confirm){//点击确定
                console.log("您点击了确定")
                wx.request({
                  url: ServiceUrl + 'managePass',
                  data:{
                    order_id: that.data.order_id,
                    user_id: that.data.user_id
                  },
                  success:function(res){
                    console.log(res);
                    if(res.data.state == 200){
                      that.sendMsgToBoss(that.data.order_id);//调起发送订阅消息的通知,管理员通过了发给老汤消息，设置金额
                      that.setData({active:2})
                      //提示审核通过信息
                      wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                      })
                      that.setData({new_state:1})
                    }else{
                      wx.showToast({
                        title: res.data.msg,
                        icon:'none'
                      })
                    }
                  },
                  fail:function(res){
                    wx.showModal({
                      title:"提示",
                      content:"发送请求失败",
                      showCancel: false,
                      confirmColor: 'skyblue',
                    })
                  }
                })
            }
        }
 })
},
//公司管理员|老汤 不通过的原因
manageNoPass:function(){
  let that=this;
  that.setData({putFrame:true});
},
cancel:function(){
  let that=this;
  that.setData({putFrame:false});
},
//初始化富文本编辑器
onEditorReady() {
  const that = this
  wx.createSelectorQuery().select('#editor').context(function (res) {
    that.editorCtx = res.context
  }).exec()
},
 
//获取编辑器输入的内容
// getContent() {
//   const that = this
//   that.editorCtx.getContents({
//     success: function (res) {  
//       console.log(res.text)
//       return res.text
//     },
//     fail: function (error){
//     	console.log(error)
//     }
//   })
// },

//管理员|老汤不通过  发送后台数据
bindFormsubmit:function(e)
 {
   let that = this
   console.log(e.detail.value.replyMsg)
   let reason = e.detail.value.replyMsg
   wx.showModal({
        title: '提示',
        content: '您确定要拒绝通过吗？',
        showCancel: true, //是否显示取消按钮-----》false去掉取消按钮
        cancelText: "否", //默认是“取消”
        cancelColor: 'skyblue', //取消文字的颜色
        confirmText: "是", //默认是“确定”
        confirmColor: 'skyblue', //确定文字的颜色
        success: function(res) {
            if (res.cancel) {//点击取消
                console.log("您点击了取消")
            } else if(res.confirm){//点击确定
                console.log("您点击了确定")                        
                //调起发送回复数据的动作
                that.finishRefuse(reason);
            }
        }
 })
},
manageRefuse:function(order_id){
  let that=this;
  //调起获取编辑器中获取的内容
  wx.request({
    url: ServiceUrl + 'sendMsgRefuse',
    data: {order_id: order_id},
    method: 'get',
    success: function(res){
      console.log(res)
      if(res.data.state==200){
        console.log(res.data.msg)
      }
    }
  })
},
//拒绝审核
finishRefuse: function(reason){
  let that = this
  wx.request({
    url: ServiceUrl + 'manageNoPass',
    data: {
      order_id: that.data.order_id,
      user_id: that.data.user_id,
      reason: reason
    },
    success: function(res){
      console.log(res)
      if(res.data.state == 200){
        wx.showToast({
          title: res.data.msg,
          icon: "none",
          duration: 2000
        })
        that.setData({
          putFrame:false,
          new_state:6,
          reason: reason
        })
        if(that.data.role_state == 1){
          //如果是老汤拒绝的，则给管理员发送拒绝通知
          that.manageRefuse(that.data.order_id)
        }
        that.setData({putFrame:false});//关闭弹窗
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: "none",
          duration: 2000
        })
      }
    },
    fail:function(res){
      wx.showModal({
        title:"提示",
        content:"发送请求失败",
        showCancel: false,
        onfirmColor: 'skyblue',
      })
    }
  })
},
/**
 * 重新预约
 */
recovery:function(){
  wx.navigateBack({
    delta: 2,
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that=this;
    that.getOrderInfo(that.data.order_id);//发送请求数据获取订单信息
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 3000);
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  /**
   * 取消订单
   */
  cancelOrder:function(){
    let that = this
    console.log('订单id为：'+that.data.order_id)
    wx.showModal({
          title: '提示',
          content: '您确定要取消订单吗？',
          showCancel: true, //是否显示取消按钮-----》false去掉取消按钮
          cancelText: "否", //默认是“取消”
          cancelColor: 'skyblue', //取消文字的颜色
          confirmText: "是", //默认是“确定”
          confirmColor: 'skyblue', //确定文字的颜色
          success: function(res) {
              if (res.cancel) {//点击取消
                  console.log("您点击了取消")
              } else if(res.confirm){//点击确定
                  console.log("您点击了确定")
                    wx.request({
                    url: ServiceUrl + 'cancelOrderAssistance',
                    data:{order_id:that.data.order_id},
                    success:function(res){
                        wx.showToast({
                          title: res.data.message,
                        })
                        wx.navigateBack({
                          delta: 0,
                        })
                    },
                    fail:function(res){
                      wx.showModal({
                        title:"提示",
                        content:"发送请求失败(取消失败)",
                        showCancel:false
                      })
                    }
                  })
              }
          },
      fail:function(res){
        wx.showModal({
          title:"提示",
          content:"发送请求失败",
          showCancel:false
        })
      }
   })
      
    
  },
  /**
   * 删除订单
   */
  deleteOrder:function(){
    let that = this
    console.log('订单id为：'+that.data.order_id)
    wx.showModal({
          title: '提示',
          content: '您确定要删除订单吗？',
          showCancel: true, //是否显示取消按钮-----》false去掉取消按钮
          cancelText: "否", //默认是“取消”
          cancelColor: 'skyblue', //取消文字的颜色
          confirmText: "是", //默认是“确定”
          confirmColor: 'skyblue', //确定文字的颜色
          success: function(res) {
              if (res.cancel) {//点击取消
                  console.log("您点击了取消")
              } else if(res.confirm){//点击确定
                  console.log("您点击了确定")
                  wx.request({
                    url: ServiceUrl + 'deleteOrderAssistance',
                    data:{order_id:that.data.order_id},
                    success:function(res){
                        wx.showToast({
                          title: res.data.message,
                          duration:1500,
                          success:function(){
                            setTimeout(function(){
                              wx.navigateBack({
                                delta: 1,
                              })
                            },1500)
                          }
                        })
                    },
                    fail:function(res){
                      wx.showModal({
                        title:"提示",
                        content:"发送请求失败",
                        showCancel:false
                      })
                    }
                  })
              }
          }
   })   
  },
})